function Role() {

}

Role.init = function () {
    let vm = new Vue({
        el: "#customModule",
        data: {
            roleChecked: "-1",
            roleList: "",
            roleNameCondition: "",
            //data start
            description: "",
            roleName: "",
            //data end
            roleIDModel: "",
            clickState: "",
            roleID: "",
            isAdd: ""
        },
        created() {
            this.roleID = $.cookie('RoleIds');
            self = this;
            window.edit = self.edit;
            window.del = self.del;
            window.detail = self.detail;
        },
        mounted() {
            this.initRole();
            this.initBootStrapSource();
        },
        methods: {
            /**
             * 获取当前角色下，可供选择的角色
             */
            initRole() {
                let url = "/RoleManage/initRole?roleID=" + $.cookie('RoleIds');
                axios.get(url).then(data => {
                    if (data.data.code === "200") {
                        this.roleList = [];
                        for (let i = 0; i < data.data.data.length; i++) {
                            let result = data.data.data[i];
                            let param = {
                                id: result.roleID,
                                name: result.roleName
                            };
                            this.roleList.push(param);
                        }
                    }
                }).catch(e => {

                })
            },
            initBootStrapSource() {
                $('#tableList').bootstrapTable({
                    url: '/RoleManage/tableSearch',
                    method: 'post',
                    toolbar: "#toolbar",
                    sidePagination: 'server',
                    dataField: "data",
                    striped: true,
                    pagination: true,
                    pageSize: "10",
                    pageList: [10, 25, 50, 100],
                    sortable: true,
                    sortName: 'roleID',
                    sortOrder: "asc",
                    search: false,
                    showRefresh: true,
                    showColumns: true,
                    showExport: true,
                    showToggle: true,
                    queryParams: function queryParams(params) {
                        return {
                            pageSize: params.limit,                         //页面大小
                            pageIndex: (params.offset / params.limit) + 1,   //页码
                            sort: params.sortName,      //排序列名
                            sortOrder: params.sortOrder, //排位命令（desc，asc）
                            roleID: $('#roleChecked').val(),
                            roleName: $('#roleNameCondition').val(),
                            currentRoleID: $.cookie('RoleIds')
                        };
                    },
                    onLoadSuccess: function (data) {
                        Help.alertSuccMessage("数据加载完毕");
                    },
                    onLoadError: function (status) {
                    },
                    columns: [
                        {
                            title: '序号',//标题  可不加
                            align: "center",
                            width: 40,
                            formatter: function (value, row, index) {
                                var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                                var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                                return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                            }
                        },
                        {field: 'roleID', title: '角色编号', align: 'center', sortable: true},
                        {field: 'roleName', title: '角色名称', align: 'center', sortable: true},
                        {field: 'description', title: '角色描述', align: 'center', sortable: true},
                        {
                            title: '操作', align: 'center',
                            formatter: function (value, row, index) {
                                let optionHtml;
                                for (let i = 0; i < row.buttonList.length; i++) {
                                    let button = row.buttonList[i];
                                    if (button.enDesc === "Select")
                                        optionHtml = " <a class=\"btn btn-info btn-xs\" href=\"javascript:void(0)\"  onclick='detail(" + JSON.stringify(row) + ")' title=\"详细\"><i class=\"fa fa-search\"></i>详细</a>";
                                    else if (button.enDesc === "Delete")
                                        optionHtml = optionHtml + " <a class=\"btn btn-danger btn-xs\" href=\"javascript:void(0)\"  onclick='del(" + JSON.stringify(row) + ")' title=\"删除\"><i class=\"fa fa-remove\"></i>删除</a>";
                                    else if (button.enDesc === "Update")
                                        optionHtml = optionHtml + "<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='edit(" + JSON.stringify(row) + ")'  title=\"修改\" ><i class=\"fa fa-edit\"></i>修改</a>";
                                    else vm.isAdd = button.enDesc === "Insert";
                                }
                                return optionHtml;
                            }
                        }
                    ]
                });
            },
            /**
             * 删除
             * @param data
             */
            del(data) {
                if (data.roleCheckName === "superAdmin" || data.roleCheckName === "admin" || data.roleCheckName === "GeneralAgent" || data.roleCheckName === "DistributionAgent") {
                    Help.alertWarnMessage("提示", "该角色为系统初始角色，不允许删除");
                    return;
                }
                swal({
                    title: "确认是否删除",
                    text: "是否确认要删除该条记录？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "删除",
                    cancelButtonText: "取消",
                    closeOnConfirm: false
                }, function () {
                    let url = "/RoleManage/del";
                    let pm = {
                        roleID: data.roleID
                    };
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            swal("删除成功!", "请继续操作", "success");
                            vm.refresh();
                        } else {
                            swal("删除失败!", "请重试", "error");
                        }
                    }).catch(e => {
                        console.log(e);
                    })
                });
            },
            /**
             * 查询
             */
            search() {
                vm.refresh();
            },
            /**
             * 重置
             */
            reset() {
                vm.roleChecked = "1";
                vm.roleNameCondition = "";
            },
            /**
             * 新增
             */
            addRecord() {
                vm.roleChecked = "";
                vm.description = "";
                vm.roleIDModel = "";
                vm.roleName = "";
                vm.clickState = "add";
                $('#myModalAdd').modal('show');
            },
            /**
             * 编辑
             * @param data
             */
            edit(data) {
                vm.roleChecked = data.roleChecked;
                vm.description = data.description;
                vm.roleIDModel = data.roleID;
                vm.roleName = data.roleName;
                vm.clickState = "edit";
                $('#myModalAdd').modal('show');
            },
            /**
             * 保存
             */
            saveRecord() {
                let pm = {
                    roleID: vm.roleIDModel,
                    description: vm.description,
                    roleName: vm.roleName,
                };
                let url = "";
                if (vm.clickState === "edit") {
                    url = "/RoleManage/edit";

                } else if (vm.clickState === "add") {
                    url = "/RoleManage/add";
                }

                axios.post(url, pm).then(data => {
                    if (data.data.code === "200") {
                        Help.alertInfoMessage("提交成功", "初始密码：123456");
                        this.refresh();
                    }
                }).catch(e => {
                    console.log(e);
                }).finally(() => {
                    $('#myModalAdd').modal('hide')
                })
            },
            /**
             * 详情
             */
            detail(data) {
                vm.roleChecked = data.roleChecked;
                vm.description = data.description;
                vm.roleIDModel = data.roleID;
                vm.roleName = data.roleName;
                vm.clickState = "view";
                $('#myModalAdd').modal('show');
            },
            refresh() {
                $('#tableList').bootstrapTable(('refresh'));
            }
        }
    });
};